//
//  KYNavVc.m
//  KYDemo
//
//  Created by 李岚清 on 16/5/21.
//  Copyright © 2016年 李岚清. All rights reserved.
//

#import "NQNavVc.h"
#import "AppDelegate.h"
#import "NQDrawerManager.h"

@interface NQNavVc ()
/**
 *  记录是否打开侧滑
 */
@property (nonatomic, assign) BOOL isOpen;


@end

@implementation NQNavVc
#pragma mark - 当前类或者他的子类第一次使用时调用
+ (void)initialize{
    //取出所有导航条
    UINavigationBar *bar = [UINavigationBar appearance];
    
    //修改导航栏颜色
    [bar setBackgroundImage:[UIImage imageNamed:@"nav"] forBarMetrics:UIBarMetricsDefault];
    
    //修改返回按钮颜色
    bar.tintColor = [UIColor whiteColor];
//    bar.barTintColor = [UIColor whiteColor];
    //普通状态下的标题属性 -- 注意:[UINavigationItem appearance]不能设置导航条上文字颜色;
    NSDictionary *attrs = @{
                            NSFontAttributeName : [UIFont fontWithName:@"AppleGothic" size:20],
                            NSForegroundColorAttributeName : [UIColor whiteColor],
                            };
    
    [bar setTitleTextAttributes:attrs];
    
}

/**
 *  判断是否打开菜单侧滑
 */
+ (void)judgeMenuOpen{
    NQNavVc *naVc = [[self alloc] init];
    if (naVc.isOpen == YES) {
        [NQDrawer close];
    }else{
        [NQDrawer open];
    }

}

#pragma mark - 控制器View的生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    //设置默认值
    self.isOpen = NO;
}

#pragma mark - ICSDrawerControllerPresenting

- (void)drawerControllerWillOpen:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = NO;
    
}

- (void)drawerControllerDidOpen:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = YES;
    self.isOpen = YES;
}

- (void)drawerControllerWillClose:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = NO;
}

- (void)drawerControllerDidClose:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = YES;
    self.isOpen = NO;
}


- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    if (self.childViewControllers.count > 0) {
        
        //设置控制器导航栏的左边按钮
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"nav_button_back_01"] forState:UIControlStateNormal];
        //尺寸自适应
        [button sizeToFit];
        //让按钮的内容往左边偏移30
        button.contentEdgeInsets = UIEdgeInsetsMake(0, -30, 0, 0);
        
        //添加"返回上层控制器"的点击事件
        [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        //包装按钮设置为导航栏左上角按钮
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    }
    
    [super pushViewController:viewController animated:animated];
}

- (void)back {
    
    [self popViewControllerAnimated:YES];

}
@end
